package com.shixianchen.study.day4;



/**
 * @author ShiXianChen
 * @version V1.0.0
 * @Description 选择排序
 * @date 2022/11/2 22:06
 */
public class SelectionSort  {


    public static void main(String[] args) {
        int[] arr = {5,4,6,7,8,9,0,1,3,2};

        for (int i = 0; i < arr.length-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < arr.length; j++) {
               minIndex = arr[minIndex] > arr[j] ? j : minIndex;
            }

            swap(arr,i,minIndex);
            print(arr);
        }
    }

    /**
     * 交换
     * @param arr
     * @param i
     * @param minIndex
     */
    private static void swap(int[] arr, int i, int minIndex) {
        int swap = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = swap;
    }

    /**
     * 打印
     * @param arr
     */
    private static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        System.out.println("==================");
    }


}
